package com.bit.musicserver.Service;

import com.bit.musicserver.Tools.ResponseBodyMessage;
import com.bit.musicserver.mapper.UserMapper;
import com.bit.musicserver.model.User;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
@Controller
public class UserService {
    @Autowired
    private UserMapper userMapper;

    public ResponseBodyMessage loginservice(@RequestParam("username") String username, @RequestParam("password") String password
            , HttpServletRequest request, HttpServletResponse response) {
        User userLogin = new User();
        userLogin.setUsername(username);
        userLogin.setPassword(password);
        List<User> user = (List) userMapper.selectAll2(username, password);
        if (!user.isEmpty() && user.size() != 0) {
            if (userLogin.getUsername().equals(user.get(0).getUsername()) &&
                    userLogin.getPassword().equals(user.get(0).getPassword())) {

                request.getSession().setAttribute("USERINDFO_SESSION_KEY", user);
                System.out.println("登录成功");
                return new ResponseBodyMessage<>(0, "登录成功", true);
            } else {
                System.out.println("用户或者密码错误， 登录失败");
                return new ResponseBodyMessage<>(-1, "登录失败", false);
            }
        } else {
            System.out.println("用户或密码为空，登录失败");
            return new ResponseBodyMessage<>(-1, "用户或密码为空", false);
        }
       // return new ResponseBodyMessage<>(-1,"用户或密码为空",false);
    }
}